<template>
  <div class="device-manager">
    <div class="manager-header">
      <h2>设备管理</h2>
      <el-button type="primary" @click="showAddDialog = true">
        <el-icon><Plus /></el-icon>
        添加设备
      </el-button>
    </div>
    
    <!-- 添加设备对话框 -->
    <el-dialog
      v-model="showAddDialog"
      title="添加新设备"
      width="500px"
      :before-close="handleClose"
    >
      <el-form :model="newDevice" label-width="80px">
        <el-form-item label="设备类型">
          <el-select v-model="newDevice.type" placeholder="请选择设备类型">
            <el-option label="灯光" value="light" />
            <el-option label="空调" value="air_conditioner" />
            <el-option label="窗帘" value="curtain" />
            <el-option label="摄像头" value="camera" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="设备名称">
          <el-input v-model="newDevice.name" placeholder="请输入设备名称" />
        </el-form-item>
        
        <el-form-item label="所在房间">
          <el-select v-model="newDevice.room" placeholder="请选择房间">
            <el-option label="客厅" value="客厅" />
            <el-option label="卧室" value="卧室" />
            <el-option label="厨房" value="厨房" />
            <el-option label="卫生间" value="卫生间" />
            <el-option label="书房" value="书房" />
          </el-select>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="addDevice">确定</el-button>
      </template>
    </el-dialog>
    
    <!-- 设备统计 -->
    <div class="device-stats">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon light">
                <el-icon><Lightning /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ devicesByType.light?.length || 0 }}</h3>
                <p>灯光设备</p>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon air">
                <el-icon><WindPower /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ devicesByType.air_conditioner?.length || 0 }}</h3>
                <p>空调设备</p>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon curtain">
                <el-icon><Grid /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ devicesByType.curtain?.length || 0 }}</h3>
                <p>窗帘设备</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon camera">
                <el-icon><VideoPlay /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ devicesByType.camera?.length || 0 }}</h3>
                <p>摄像头设备</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon active">
                <el-icon><Switch /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ activeDevicesCount }}</h3>
                <p>运行中</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    
    <!-- 设备列表 -->
    <div class="device-list">
      <div v-if="devices.length === 0" class="empty-state">
        <el-empty description="暂无设备，请添加设备">
          <el-button type="primary" @click="showAddDialog = true">添加设备</el-button>
        </el-empty>
      </div>
      
      <div v-else>
        <!-- 按房间分组显示 -->
        <div v-for="(roomDevices, room) in devicesByRoom" :key="room" class="room-section">
          <h3 class="room-title">
            <el-icon><House /></el-icon>
            {{ room }} ({{ roomDevices.length }})
          </h3>
          
          <div class="devices-grid">
            <LightDevice
              v-for="device in roomDevices.filter(d => d.type === 'light')"
              :key="device.id"
              :device="device"
              @update:device="updateDevice"
            />
            
            <AirConditionerDevice
              v-for="device in roomDevices.filter(d => d.type === 'air_conditioner')"
              :key="device.id"
              :device="device"
              @update:device="updateDevice"
            />
            
            <CurtainDevice
              v-for="device in roomDevices.filter(d => d.type === 'curtain')"
              :key="device.id"
              :device="device"
              @update:device="updateDevice"
            />

            <CameraDevice
              v-for="device in roomDevices.filter(d => d.type === 'camera')"
              :key="device.id"
              :device="device"
              @update:device="updateDevice"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
// eslint-disable-next-line no-unused-vars
import { Plus, Lightning, WindPower, Grid, Switch, House, VideoPlay } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useDeviceStore } from '../store/deviceStore.js'
import LightDevice from './devices/LightDevice.vue'
import AirConditionerDevice from './devices/AirConditionerDevice.vue'
import CurtainDevice from './devices/CurtainDevice.vue'
import CameraDevice from './devices/CameraDevice.vue'

export default {
  name: 'DeviceManager',
  components: {
    LightDevice,
    AirConditionerDevice,
    CurtainDevice,
    CameraDevice
  },
  setup() {
    const deviceStore = useDeviceStore()
    const showAddDialog = ref(false)
    
    const newDevice = reactive({
      type: '',
      name: '',
      room: '客厅'
    })
    
    const addDevice = async () => {
      if (!newDevice.type || !newDevice.name) {
        ElMessage.warning('请填写完整信息')
        return
      }
      
      try {
        await deviceStore.addDevice(newDevice.type, newDevice.name, newDevice.room)
        ElMessage.success('设备添加成功')
        
        // 重置表单
        newDevice.type = ''
        newDevice.name = ''
        newDevice.room = '客厅'
        showAddDialog.value = false
      } catch (error) {
        ElMessage.error('设备添加失败：' + error.message)
      }
    }
    
    const handleClose = () => {
      showAddDialog.value = false
    }
    
    const updateDevice = (updatedDevice) => {
      // 更新设备状态，这里设备状态已经在deviceStore中管理
      // 这个方法主要是为了处理emit事件，实际更新由deviceStore处理
      console.log('设备更新事件:', updatedDevice)
    }
    
    return {
      showAddDialog,
      newDevice,
      addDevice,
      handleClose,
      updateDevice,
      devices: deviceStore.devices,
      devicesByRoom: deviceStore.devicesByRoom,
      devicesByType: deviceStore.devicesByType,
      activeDevicesCount: deviceStore.activeDevicesCount
    }
  }
}
</script>

<style scoped>
.device-manager {
  padding: 20px;
}

.manager-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.manager-header h2 {
  margin: 0;
  color: #333;
}

.device-stats {
  margin-bottom: 30px;
}

.stat-card {
  border-radius: 12px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 24px;
  color: white;
}

.stat-icon.light {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
}

.stat-icon.air {
  background: linear-gradient(135deg, #409eff, #66b3ff);
}

.stat-icon.curtain {
  background: linear-gradient(135deg, #e6a23c, #f0c78a);
}

.stat-icon.camera {
  background: linear-gradient(135deg, #9c27b0, #ba68c8);
}

.stat-icon.active {
  background: linear-gradient(135deg, #13ce66, #67c23a);
}

.stat-info h3 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.stat-info p {
  margin: 5px 0 0 0;
  color: #666;
  font-size: 14px;
}

.device-list {
  margin-top: 20px;
}

.empty-state {
  text-align: center;
  padding: 60px 0;
}

.room-section {
  margin-bottom: 40px;
}

.room-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.room-title .el-icon {
  margin-right: 10px;
  color: #409eff;
}

.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
}

@media (max-width: 768px) {
  .devices-grid {
    grid-template-columns: 1fr;
  }
  
  .manager-header {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
}
</style>
